﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GsOrganization.Common.Extend
{
    /// <summary>
    /// JSON 扩展方法类
    /// </summary>
    public static class JsonExtend
    {
        public static string ToJsonString<T>(this T jsonObject)
        {
            if (jsonObject == null)
                return "";
            var setting = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatString = "yyyy-MM-dd HH:mm:ss" };
            var json = JsonConvert.SerializeObject(jsonObject, setting);
            return json;
        }

        public static T ToJsonObject<T>(this string objString) where T : class
        {
            T result = default!;
            try
            {
                if (!string.IsNullOrEmpty(objString))
                    result = JsonConvert.DeserializeObject<T>(objString);
                else
                    result = default!;
            }
            catch (Exception ex)
            {
                result = default!;
            }
            return result!;
        }
    }
}
